﻿Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Data.SqlClient
Imports Ionic.Zip
Partial Class Tools_Backup
    Inherits System.Web.UI.Page

    'Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    '    Dim obj As New Authorization
    '    Dim url As String = Request.AppRelativeCurrentExecutionFilePath

    '    Dim id As String = obj.GetMenuID(url)
    '    Dim is_Authorized As Boolean = obj.CheckAuthorization(id)

    '    If is_Authorized = True Then
    '        'User Authorized to See that page .. Continue
    '    Else
    '        'User is not Authorized 
    '        Response.Redirect("~/UnAuthorized.aspx")
    '    End If


    'End Sub


    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")
        End If
    End Sub


    Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub ibtnBackupDB_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnBackupDB.Click
        Response.Clear()
        Response.BufferOutput = False

        Dim archiveName As String = "DBbackup.zip"
        Response.ContentType = "application/zip"
        Response.AddHeader("content-disposition", "filename=" + archiveName)

        Dim backupObj As New DBBackupSP
        Dim bakupDS As New Data.DataSet
        bakupDS = backupObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "Backup")

        Using zip As New ZipFile()
            'zip.AddDirectory(Server.MapPath("~/Assets/Upload/"), "httpdocs/Assets/Upload")
            'zip.AddDirectory(Server.MapPath("~/App_Data/"), "httpdocs/App_Data")
            zip.AddFile(bakupDS.Tables(0).Rows(0).Item(0))
            zip.Save(Response.OutputStream)
        End Using
        Response.Close()
    End Sub
End Class